package top.wherewego.vnt.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class CustomBtnView extends View {

    private Paint circlePaint;
    private float circleRadius;
    private float maxCircleRadius;
    private float centerX;
    private float centerY;
    private boolean isAnimating = false;
    private float alpha = 255;

    public CustomBtnView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        circlePaint = new Paint();
        circlePaint.setColor(Color.GRAY);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(5);

//        circleRadius = 200;
//        maxCircleRadius = 300;
    }

    private void startAnimation() {
        isAnimating = true;
        circlePaint.setColor(Color.BLUE);
        ValueAnimator animator = ValueAnimator.ofFloat(circleRadius, maxCircleRadius);
        animator.setDuration(1500);
        animator.addUpdateListener(animation -> {
            circleRadius = (float) animation.getAnimatedValue();
            alpha = 255 - (circleRadius / maxCircleRadius) * 255; // 根据扩散距离计算透明度
            invalidate();
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                isAnimating = false;
                circleRadius = 150; // 重置圆环半径
                alpha = 255; // 重置透明度
                startAnimation(); // 循环执行动画
            }
        });
        animator.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

//        int widthInPx = getWidth();
//        int heightInPx = getHeight();
//        DebugLogG.LogI("widthInPx: " + widthInPx);
//        DebugLogG.LogI("heightInPx: " + heightInPx);

        circleRadius = getWidth() / 4;
        maxCircleRadius = getWidth() / 3;
//        startAnimation();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        centerX = getWidth() / 2;
        centerY = getHeight() / 2;

        // 绘制淡化效果的圆环
        circlePaint.setAlpha((int) alpha);
        canvas.drawCircle(centerX, centerY, circleRadius, circlePaint);
    }
}
